﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EntityFramework.Extensions;
using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Kugar.MVC.DaemonUI;
using Kugar.MVC.DaemonUI.Controllers;
using Kugar.Web.MVC;
using WS.FlowAuth.Web.Areas.Admin.Filters;
using WS.FlowAuth.Web.BLL;
using WS.FlowAuth.Web.Data.EF;

namespace WS.FlowAuth.Web.Areas.Admin.Controllers
{
    [UserAuthority]
    public class UserCodeController : AdminBaseController
    {
        // GET: Admin/UserCode
        public ActionResult List()
        {
            var pageIndex = Request.GetInt("pageIndex", 1);
            var keyword = Request.GetString("Keyword", "");
            var startDt = Request.GetDateTimeNullable("StartDt", "yyyy-MM-dd", null);
            var endDt = Request.GetDateTimeNullable("EndDt", "yyyy-MM-dd", null);

            startDt = startDt?.SetStartTime();
            endDt = endDt?.SetEndTime();

            var lst = Context.base_UserCode
                .Where(x => //!x.IsDeleted && 
                            (keyword == "" ||
                                             (x.UserCode.Contains(keyword) || x.CustomerName.Contains(keyword))) &&
                            (startDt==null || x.ExpireDt>=startDt) &&
                            (endDt==null || x.ExpireDt<=endDt)
                        )
                .OrderByDescending(x=>x.CreateDt)
                .GetPagedList(pageIndex, 20);

            ViewBag.List = lst;
            ViewBag.Keyword = keyword;
            ViewBag.StartDt = startDt;
            ViewBag.EndDt = endDt;

            return View();
        }

        [HttpGet]
        public ActionResult Detail()
        {
            var userCodeID = Request.GetInt("userCodeID", 0);

            if (userCodeID <= 0)
            {
                ViewBag.Info = new base_UserCode()
                {
                    ExpireDt = DateTime.Now.AddYears(1)
                };
            }
            else
            {
                ViewBag.Info = Context.base_UserCode.FirstOrDefault(x => x.UserCodeID == userCodeID);
            }



            return View();
        }

        [HttpPost, ActionName("Detail")]
        public ActionResult DetailPost()
        {
            var userCodeID = Request.GetInt("UserCodeID", 0);
            var userCode = Request.GetString("UserCode");
            var customerName = Request.GetString("CustomerName");
            var expireDt = Request.GetDateTime("ExpireDt", "yyyy-MM-dd", DateTime.Now.AddYears(1));

            if (string.IsNullOrWhiteSpace(userCode))
            {
                ModelState.AddModelError("UserCode", "用户代码不能为空");
            }

            if (string.IsNullOrWhiteSpace(customerName))
            {
                ModelState.AddModelError("CustomerName", "客户名称不能为空");
            }


            ViewBag.Info = null;

            try
            {
                if (ModelState.IsValid)
                {
                    if (userCodeID <= 0)
                    {
                        var item = new base_UserCode()
                        {
                            UserCode = userCode,
                            ExpireDt = expireDt,
                            CreateDt = DateTime.Now,
                            CustomerName = customerName,
                            IsDeleted = false
                        };

                        Context.base_UserCode.Add(item);

                        try
                        {
                            Context.SaveChanges();

                            ViewBag.Info = Context.base_UserCode.FirstOrDefault(x => x.UserCodeID == item.UserCodeID);

                            MsgBoxAndCloseModal("添加成功", true);
                        }
                        catch (Exception e)
                        {
                            MsgBox("添加失败:" + e.Message);
                        }
                    }
                    else
                    {
                        Context.base_UserCode.Where(x => x.UserCodeID == userCodeID).Update(x => new base_UserCode()
                        {
                            UserCode = userCode,
                            CustomerName = customerName,
                            ExpireDt = expireDt
                        });

                        ViewBag.Info = Context.base_UserCode.FirstOrDefault(x => x.UserCodeID == userCodeID);

                        MsgBoxAndCloseModal("更新成功", true);
                    }
                }
                else
                {
                    MsgBox("数据错误,请检查");
                }
            }
            catch (Exception e)
            {
                MsgBox("操作失败");
            }

            if (ViewBag.Info == null)
            {
                ViewBag.Info = new base_UserCode()
                {
                    UserCode = userCode,
                    ExpireDt = expireDt,
                    CustomerName = customerName
                };
            }

            return View();
        }

        public ActionResult MethodHandler(string methodName)
        {
            var userCodeID = Request.GetInt("UserCodeID", 0);

            ResultReturn ret = null;

            switch (methodName.ToLower())
            {
                case "delete":
                {
                    Context.base_UserCode.Where(x => x.UserCodeID == userCodeID).Update(x => new base_UserCode()
                    {
                        IsDeleted = true
                    });

                    ret=new SuccessResuleReturn("删除成功");

                    break;
                }
                case "reset":
                {
                    Context.base_UserCode.Where(x => x.UserCodeID == userCodeID).Update(x => new base_UserCode()
                    {
                        IsDeleted = false
                    });

                    ret = new SuccessResuleReturn("删除成功");

                    break;
                    }
            }

            if (ret==null)
            {
                return HttpNotFound();
            }

            return this.NewtonJson(ret);
        }
    }
}